package org.itboys.seller;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.itboys.admin.constant.AdminSessionConstant;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.servlet.ServletContextHolder;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.shop.entity.ScenicSpot;
import org.itboys.shop.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 商家
 * @author weisky
 *
 */
@RestController
@RequestMapping("/scenic")
public class ScenicSpotController extends BaseController{

    @Autowired
    private ScenicSpotService scenicSpotService;

    /**
     * 跳到商家管理页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/toListVM")
    public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/seller/scenic-list");
    }

    @RequestMapping("/list")
    public void list(@Param("type")Integer type, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
        String name = (String) param.get("name");
        if(StringUtils.isNotBlank(name)){//根据名称  模糊查询
            param.put("name", AceUtils.getPattern(name));
        }
        long centerId = (Long)ServletContextHolder.getSession().getAttribute(AdminSessionConstant.CENTER_ID);
        param.put("centerId", centerId);
        Page<ScenicSpot> page = scenicSpotService.pageQuery(param, request);
        JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
    }

    /**
     * 新增或者修改
     */
    @RequestMapping("/save")
    public void save(ScenicSpot spot,
                     @RequestParam(required=false) MultipartFile imgFile,
                     @RequestParam(value="imgFiles", required = false) MultipartFile[] imgFiles,
                     HttpServletRequest request, HttpServletResponse response){

        long centerId = (Long)ServletContextHolder.getSession().getAttribute(AdminSessionConstant.CENTER_ID);
        spot.setCenterId(centerId);
        boolean result = scenicSpotService.insert(spot, imgFile, imgFiles);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 获取一条数据
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/getById")
    public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
        AjaxUtils.renderJson(response, scenicSpotService.getById(id));
    }

    /**
     * 删除一条数据
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
        boolean result = scenicSpotService.dele(id);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 异步删除
     */
    @RequestMapping("/deleteImage/{id}")
    public void deleteImage(@PathVariable("id") Long id, HttpServletResponse response) {
        try {
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("AdminUser deleteImage error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 返回所有列表
     * @param response
     * @param request
     */
    @RequestMapping("/allTouristList")
    public void allList(HttpServletResponse response,
                        HttpServletRequest request) {
        final Map<String, Object> sqlMap = QueryParamUtils.builderQueryMap(request);
        long centerId = (Long) ServletContextHolder.getSession().getAttribute(AdminSessionConstant.CENTER_ID);
        sqlMap.put("centerId", centerId);
        List<ScenicSpot> list = scenicSpotService.list(sqlMap);
        AjaxUtils.renderJson(response, list);
    }


}
